<%
  # Redmine plugin for Document Management System "Features"
  #
  # Karel Pičman <karel.picman@kontron.com>
  #
  # This program is free software; you can redistribute it and/or
  # modify it under the terms of the GNU General Public License
  # as published by the Free Software Foundation; either version 2
  # of the License, or (at your option) any later version.
  #
  # This program is distributed in the hope that it will be useful,
  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  # GNU General Public License for more details.
  #
  # You should have received a copy of the GNU General Public License
  # along with this program; if not, write to the Free Software
  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
%>

<% path = @dmsf_workflow.project ? settings_project_path(@project, tab: 'dmsf_workflow') : dmsf_workflows_path %>
<h2><%= link_to l(:label_dmsf_workflow_plural), path %> &#187; <%=h @dmsf_workflow %></h2>

<%= labelled_form_for @dmsf_workflow do |f| %>
  <%= hidden_field_tag :back_url, dmsf_workflow_path(@dmsf_workflow) %>
  <%= error_messages_for 'workflow' %>
  <div class="box tabular">
    <p>
      <%= f.text_field :name, required: true %>
      <%= f.submit l(:button_save) %>
    </p>
  </div>
<% end %>

<div id="tab-content-members">
  <p>
    <%= link_to sprite_icon('add', l(:dmsf_new_step_or_approver)), new_step_dmsf_workflow_path(@dmsf_workflow),
                remote: true, class: 'icon icon-add' %>
  </p>
  <% steps = @dmsf_workflow.dmsf_workflow_steps.collect{|s| s.step}.uniq %>
  <% if steps.any? %>
    <table class="list steps">
      <thead><tr>
        <th></th>
        <th><%= l(:label_dmsf_workflow_step) %></th>
        <th><%= l(:label_dmsf_workflow_approval_plural) %></th>
        <th></th>
      </tr></thead>
      <tbody>
      <% steps.each do |i| %>
        <tr id="step-<%= i %>" class="step">
          <% index = @dmsf_workflow.dmsf_workflow_steps.order(:id).index{ |s| s.step == i } %>
          <td class="id"><%= i %></td>
          <td class="name">
            <span id="step-index-<%= index %>-name"><%= @dmsf_workflow.dmsf_workflow_steps[index].name if index %></span>
            <div id="<%= "step-index-#{index}-name-form" %>">
              <%= form_for(@dmsf_workflow, url: update_step_dmsf_workflow_path(step: index), method: :put,
                           html: { id: "step-index-name-#{index}-form" }) do |f| %>
                <%= hidden_field_tag :back_url, dmsf_workflow_path(@dmsf_workflow) %>
                <%= f.text_field(:step_name, value: @dmsf_workflow.dmsf_workflow_steps[index].name,
                      id: "dmsf_workflow_step_name_#{index}") %>
                <p>
                  <%= submit_tag l(:button_change), class: 'small' %>
                  <%= link_to_function(
                          l(:button_cancel),
                          "$('#step-index-#{index}-name').show(); $('#step-index-#{index}-approvers').show(); $('#step-index-#{index}-name-form').hide(); $('#step-index-#{index}-approvers-form').hide();")
                  %>
                </p>
              <% end %>
            </div>
          </td>
          <td>
            <span id="step-index-<%= index %>-approvers">
              <% stps = @dmsf_workflow.dmsf_workflow_steps.order(operator: :desc).
                collect{ |s| (s.step == i) ? s : nil }.compact %>
              <% stps.each_with_index do |step, j| %>
                <% if (j > 0) || (step.operator != DmsfWorkflowStep::OPERATOR_AND) %>
                  <b><%= step.soperator %></b>
                <% end %>
                <%= link_to_user step.user %>
              <% end %>
            </span>
            <div id="<%= "step-index-#{index}-approvers-form" %>">
              <%= form_for(@dmsf_workflow, url: update_step_dmsf_workflow_path(step: index), method: :put,
                           html: { id: "step-index-operator-#{index}-form"}) do |_| %>
                <%= hidden_field_tag :back_url, dmsf_workflow_path(@dmsf_workflow) %>
                <div class="dmsf-parent-container">
                  <% stps.each do |step| %>
                      <div class="dmsf-child-container">
                        <label>
                          <%= radio_button_tag "operator_step[#{step.id}]", DmsfWorkflowStep::OPERATOR_AND,
                            step.operator == DmsfWorkflowStep::OPERATOR_AND %>
                          <%= DmsfWorkflowStep.soperator(DmsfWorkflowStep::OPERATOR_AND) %>
                        </label>
                        <br>
                        <label>
                          <%= radio_button_tag "operator_step[#{step.id}]", DmsfWorkflowStep::OPERATOR_OR,
                            step.operator == DmsfWorkflowStep::OPERATOR_OR %>
                          <%= DmsfWorkflowStep.soperator(DmsfWorkflowStep::OPERATOR_OR) %>
                        </label>
                        <br>
                        &nbsp;<%= delete_link delete_step_dmsf_workflow_path(step: step.id) %>
                      </div>
                      <div class="dmsf-child-container">
                        <%= select_tag "assignee[#{step.id}]",
                                        principals_options_for_select(@approving_candidates | [step.user], step.user),
                                        include_blank: false %>
                      </div>
                  <% end %>
                </div>
                <p>
                  <%= submit_tag l(:button_change), class: 'small' %>
                  <%= link_to_function l(:button_cancel),
                        "$('#step-index-#{index}-name').show(); $('#step-index-#{index}-approvers').show(); $('#step-index-#{index}-name-form').hide(); $('#step-index-#{index}-approvers-form').hide();"
                  %>
                </p>
                <% end %>
            </div>
            </td>
            <td class="buttons">
              <%= reorder_handle(@dmsf_workflow, url: url_for(action: 'edit', id: @dmsf_workflow, step: i) ) %>
              <%= link_to_function sprite_icon('edit', l(:button_edit)),
                     "$('#step-index-#{index}-name').hide(); $('#step-index-#{index}-approvers').hide(); $('#step-index-#{index}-name-form').show(); $('#step-index-#{index}-approvers-form').show();",
                     class: 'icon icon-edit' %>
              <%= delete_link edit_dmsf_workflow_path(@dmsf_workflow, step: i,
                    back_url: dmsf_workflow_path(@dmsf_workflow)) %>
            </td>
          <% end %>
        </tr>
      </tbody>
    </table>
  <% else %>
    <p class="nodata"><%= l(:label_no_data) %></p>
  <% end %>
</div>

<%= late_javascript_tag do %>
    $(function() { $("table.steps tbody").positionedItems(); });
<% end %>
